package com.gadgets.cm05.common.dto;

import com.gadgets.framework.common.exception.GadgetsValidateException;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能说明：
 * 著作者：修峻青
 * 创建日期：2023-01-07
 */
public class MathMap<T, K> extends HashMap<T, K> {

    /**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.
     *
     * @param key   key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     * <tt>null</tt> if there was no mapping for <tt>key</tt>.
     * (A <tt>null</tt> return can also indicate that the map
     * previously associated <tt>null</tt> with <tt>key</tt>.)
     */
    @Override
    public K put(T key, K value) {
        if(null==key){
            throw new GadgetsValidateException("Key不能为空");
        }
        if(null==value){
            throw new GadgetsValidateException("Value不能为空");
        }
        return super.put(key, value);
    }

    /**
     * Copies all of the mappings from the specified map to this map.
     * These mappings will replace any mappings that this map had for
     * any of the keys currently in the specified map.
     *
     * @param m mappings to be stored in this map
     * @throws NullPointerException if the specified map is null
     */
    @Override
    public void putAll(Map<? extends T, ? extends K> m) {
        if(null==m || m.size()<1){
            throw new GadgetsValidateException("集合不能为空");
        }
        for (Entry<? extends T, ? extends K> entry : m.entrySet()) {
            if(null==entry.getKey()){
                throw new GadgetsValidateException("Key不能为空");
            }
            if(null==entry.getValue()){
                throw new GadgetsValidateException("Value不能为空");
            }
        }
        super.putAll(m);
    }
}
